package com.csap.framework.apidoc.admin;

import com.csap.framework.util.time.TimeUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author yangchengfu
 * @description
 * @dataTime 2021年-03月-17日 18:32:00
 **/
@Slf4j
public class Shell {

    static class RunThread extends Thread {
        private InputStream is;
        private String type;
        private List<String> strList;

        RunThread(InputStream is, String printType, List<String> strList) {
            this.is = is;
            this.type = printType;
            this.strList = strList;
        }

        @Override
        public void run() {
            try {
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line = null;
                while ((line = br.readLine()) != null) {
                    String string = (TimeUtil.date2string(System.currentTimeMillis()) + "  " + type + " > " + line);
                    System.out.println(string);
                    strList.add(string);
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }

    /**
     * 运行shell脚本
     *
     * @param shell 需要运行的shell脚本
     */
    public static Process execShell(String shell) {
        try {
            return Runtime.getRuntime().exec(shell);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 运行shell脚本 new String[]方式
     *
     * @param shell 需要运行的shell脚本
     */
    public static void execShellBin(String shell) {
        try {
            Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", shell}, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 运行shell命令
     *
     * @param commands
     * @return
     */
    public static List<String> runShell(List<String> commands) {
        return runShell(commands, null, null, Collections.synchronizedList(new ArrayList<>()));
    }

    /**
     * 运行shell命令
     *
     * @param commands
     * @return
     */
    public static List<String> runShell(List<String> commands, List<String> strList) {
        return runShell(commands, null, null, strList);
    }

    /**
     * 运行shell命令
     *
     * @param commands
     * @param dir
     * @return
     */
    public static List<String> runShell(List<String> commands, File dir) {
        return runShell(commands, null, dir, Collections.synchronizedList(new ArrayList<>()));
    }

    /**
     * 运行shell并获得结果，注意：如果sh中含有awk,一定要按new String[]{"/bin/sh","-c",shStr}写,才可以获得流
     *
     * @param commands 需要执行的shell
     * @return
     */
    public static List<String> runShell(List<String> commands, String[] envp, File dir, List<String> strList) {
        try {
            Process process = Runtime.getRuntime().exec("/bin/bash", envp, dir);
            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(process.getOutputStream())), true);
            for (String line : commands) {
                out.println(line);
            }
            out.println("exit");
            new RunThread(process.getInputStream(), "INFO", strList).start();
            new RunThread(process.getErrorStream(), "ERROR", strList).start();
            int value = process.waitFor();
            if (value == 0) {
                System.out.println("complete");
            } else {
                System.out.println("failuer");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strList;
    }


    @SneakyThrows
    public static void main(String[] args) {
        List<String> re = runShell(Arrays.asList(
                "cd /Users/ycf/Documents/产品/csap/framework/csap-framework-apidoc/apidoc-template",
                "pwd",
                "npm run docs:build"));
        System.out.println(re);
        System.out.println("success");
    }
}
